package com.mf.ergate.controller.opt;

import com.mf.ergate.api.opt.EmployeeInfoRemoteService;
import com.mf.ergate.api.opt.model.dto.EmployeeInfoCreateDto;
import com.mf.ergate.api.opt.model.dto.EmployeeInfoDeleteDto;
import com.mf.ergate.api.opt.model.dto.EmployeeInfoQueryDto;
import com.mf.ergate.api.opt.model.dto.EmployeeInfoUpdateDto;
import com.mf.ergate.api.opt.model.vo.EmployeeInfoVo;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.domain.model.opt.EmployeeInfoEntity;
import com.mf.ergate.service.opt.EmployeeInfoService;
import com.mf.framework.common.model.Page;
import com.mf.framework.common.model.RemotePage;
import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 员工信息Controller
 * User: zhaoming
 * DateTime: 2019-04-16
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class EmployeeInfoController implements EmployeeInfoRemoteService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private EmployeeInfoService employeeInfoService;

    /**
     * 新增
     * @param employeeInfoCreateDto 员工信息新增Dto
     * @return 员工信息
     */
    public Result<EmployeeInfoVo> insert(@RequestBody EmployeeInfoCreateDto employeeInfoCreateDto) {
        EmployeeInfoEntity employeeInfoEntity = MfBeanUtils.copyObject(employeeInfoCreateDto, EmployeeInfoEntity.class);
        employeeInfoEntity = employeeInfoService.insert(employeeInfoEntity);
        EmployeeInfoVo employeeInfoVo = MfBeanUtils.copyObject(employeeInfoEntity, EmployeeInfoVo.class);
        return new Result<>(employeeInfoVo);
    }

    /**
     * 根据唯一编号更新
     * @param employeeInfoUpdateDto 员工信息更新Dto
     * @return 执行结果
     */
    public Result<Boolean> updateByUniqueNo(@RequestBody EmployeeInfoUpdateDto employeeInfoUpdateDto) {
        EmployeeInfoEntity employeeInfoEntity = MfBeanUtils.copyObject(employeeInfoUpdateDto, EmployeeInfoEntity.class);
        Boolean flag = employeeInfoService.updateByUniqueNo(employeeInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号删除(逻辑删除)
     * @param employeeInfoDeleteDto 员工信息删除Dto
     * @return 结果集
     */
    public Result<Boolean> deleteByUniqueNo(@RequestBody EmployeeInfoDeleteDto employeeInfoDeleteDto){
        EmployeeInfoEntity employeeInfoEntity = MfBeanUtils.copyObject(employeeInfoDeleteDto, EmployeeInfoEntity.class);
        Boolean flag = employeeInfoService.deleteByUniqueNo(employeeInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号查询
     * @param uniqueNo 唯一编号
     * @return 员工信息
     */
    public Result<EmployeeInfoVo> selectByUniqueNo(@RequestBody String uniqueNo) {
        EmployeeInfoEntity employeeInfoEntity = employeeInfoService.selectByUniqueNo(uniqueNo);
        EmployeeInfoVo employeeInfoVo = MfBeanUtils.copyObject(employeeInfoEntity, EmployeeInfoVo.class);
        return new Result<>(employeeInfoVo);
    }

    /**
     * 根据条件查询
     * @param employeeInfoQueryDto 员工信息查询Dto
     * @return 员工信息列表
     */
    public Result<List<EmployeeInfoVo>> selectByCondition(@RequestBody EmployeeInfoQueryDto employeeInfoQueryDto) {
        List<EmployeeInfoEntity> employeeInfoEntities = employeeInfoService.selectByCondition(employeeInfoQueryDto);
        List<EmployeeInfoVo> employeeInfoVos = MfBeanUtils.copyList(employeeInfoEntities, EmployeeInfoVo.class);
        return new Result<>(employeeInfoVos);
    }

    /**
     * 根据条件查询数量
     * @param employeeInfoQueryDto 员工信息查询Dto
     * @return 数量
     */
    public Result<Integer> selectCount(@RequestBody EmployeeInfoQueryDto employeeInfoQueryDto) {
        return new Result<>(employeeInfoService.selectCount(employeeInfoQueryDto));
    }

    /**
     * 根据条件分页查询
     * @param remotePage 远程分页对象
     * @return 员工信息分页列表
     */
    public Result<Page<EmployeeInfoVo>> selectPage(@RequestBody RemotePage<EmployeeInfoQueryDto> remotePage){
        Page<EmployeeInfoEntity> employeeInfoEntityPage = employeeInfoService.selectPage(remotePage.getPage(), remotePage.getObj());
        Page<EmployeeInfoVo> page = MfBeanUtils.copyPage(employeeInfoEntityPage, EmployeeInfoVo.class);
        return new Result<>(page);
    }

    /**
     * 删除员工及相关信息(逻辑删除)
     * @param employeeInfoDeleteDto 员工信息删除Dto
     * @return 执行结果
     */
    public Result<Boolean> deleteCompleteByUniqueNo(@RequestBody EmployeeInfoDeleteDto employeeInfoDeleteDto){
        EmployeeInfoEntity orgEmployeeInfoEntity = MfBeanUtils.copyObject(employeeInfoDeleteDto, EmployeeInfoEntity.class);
        Boolean flag = employeeInfoService.deleteCompleteByUniqueNo(orgEmployeeInfoEntity);
        return new Result<>(flag);
    }
}